<?php

require PATH_CONTRALLERS . 'PhpMysql.php';

class QuanjuPage extends MenuPage {

    private $CONN;
    private $way;

    function __construct() {
        $this->CONN = new PhpMysql();
        //new包含的类PhpMysql
        parent::getMenuList($this->CONN);
        //调父类的getMenuList方法，把$this->conn赋给$obj_conn传给getMenuList()
        $this->way = isset($_GET['way']) ? trim($_GET['way']) : '';
    }

    function quanju() {
        include "view/quanju_main.php";
    }


    function addGuanggao() {
        if ($this->way == 'A') {
            if (!$_POST) {
                $category = $this->CONN->getList('tb_admin_ad_category', 'Fid,Fname', 'ORDER BY Fsort');
                include PATH_QUANJU . "add_guanggao/add_guang.php";
            } else {
                $pids_arr = isset($_POST['pid']) ? ($_POST['pid']) : array(0);
                $data = array(
                    'FcateId' => implode($pids_arr),
                    'Fnum' => isset($_POST['Fnum']) ? trim($_POST['Fnum']) : '',
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : '',
                    'Fremark' => isset($_POST['Fremark']) ? trim($_POST['Fremark']) : '',
                    'Flink' => isset($_POST['Flink']) ? trim($_POST['Flink']) : '',
                    'Fwidth' => isset($_POST['Fwidth']) ? trim($_POST['Fwidth']) : 0,
                    'Fheight' => isset($_POST['Fheight']) ? trim($_POST['Fheight']) : 0,
                    'Fcode' => isset($_POST['Fcode']) ? trim($_POST['Fcode']) : ''
                );
                $info = $this->CONN->getOneRow('tb_admin_ad', 'Fid', "WHERE FcateId={$data['FcateId']} AND Fnum='{$data['Fnum']}'");
                if (isset($info['Fid'])) {
                    parent::getMsg('对不起，同一个广告分类下已经存在一个同样编号的广告位了。请更新一个广告编号再试。', URL);
                    exit;
                }
                //上传文件
                if (isset($_FILES['Fimage']['name']) && !empty($_FILES['Fimage']['name'])) {
                    $uploadFile = new ExtendsYspUploadFile();
                    //限制文件大小字节
                    $uploadFile->max_size = 1024 * 1024 * 2;
                    //允许上传的文件类型
                    $uploadFile->allow_type = "gif/jpg/png/png2/swf";
                    //随机文件名 (默认)
                    $uploadFile->rand_name = false;
                    //表单中的文本域名称
                    $uploadFile->input_name = "Fimage";
                    $uploadFile->save_path = PATH_UPLOAD . "ad/";
                    //重设文件名(用广告编码)
                    $uploadFile->reset_name = trim($data['Fnum']);
                    //保存文件，成功返回 true
                    if ($uploadFile->save()) {
                        $data['Fimage'] = str_ireplace(PATH_UPLOAD, '', $uploadFile->file_upload_path);
                    }
                }

                $rs = $this->CONN->Add('tb_admin_ad', $data);
                if ($rs > 0) {
                    parent::getMsg('操作成功', URL);
                } else {
                    parent::getMsg('操作失败', URL);
                }
            }
        }


        if ($this->way == 'S') {
            $page = isset($_GET['page']) ? intval($_GET['page']) : 1; //当前页码
            $pagesize = 10; //每页条数
            $page = ($page < 1) ? 1 : $page;
            $start = ($page - 1) * $pagesize; //起始条数
            //查询关键字
            $keywords = isset($_GET['query']) ? trim($_GET['query']) : '';
            $where = $keywords ? "WHERE Fname LIKE '%$keywords%' OR Fnum='$keywords' OR Fcode='$keywords'" : '';

            //取总记录数
            $total = $this->CONN->getTotal('tb_admin_ad', $where);
            $info_list = $this->CONN->getList('tb_admin_ad', '*', $where, 'ORDER BY FcateId,Fnum', "LIMIT $start,$pagesize");

            if ($total != 0) {
                $cate_ids = 0;
                foreach ($info_list as $ls) {
                    $cate_ids .= ',' . $ls['FcateId'];
                }
                //取所有菜单，构造数组
                $categroy = $this->CONN->getList('tb_admin_ad_category', 'Fid,Fname', "WHERE Fid IN ($cate_ids)");
                $cate_arr = array();
                foreach ($categroy as $ls) {
                    $cate_arr[$ls['Fid']] = $ls['Fname'];
                }
            } else {
                $pagerlink = '对不起，没有相关信息';
            }


            include PATH_QUANJU . "add_guanggao/fenyPage.php";


            include PATH_QUANJU . "add_guanggao/menu_gonggao.php";
        }
        
        
        if ($this->way == 'F') {
            $rs = $this->CONN->Del('tb_admin_ad', 'Fid');
            if ($rs !== false) {
                $href = str_replace('&way=F', '&way=S', URL);
                parent::getMsg('删除成功', $href);
            } else {
                parent::getMsg('删除失败', URL);
            }
        }
    }

    function menuAjax() {
        //接收value值
        $Pid = $_GET['pid'];
        $Zid = $_GET['zid'];
        //$Level_second[$Pid]是一个数组
        $Level_second[$Pid] = $this->CONN->getList('tb_admin_ad_category', 'Fid,Fname,FcateId,FcateIds', "WHERE FcateId=$Pid ", "ORDER BY Fsort");
        echo "<option value='0'>请选择下级</option>";
        foreach ($Level_second[$Pid] as $k => $ss) {
            echo "<option value='{$ss['Fid']}'>{$ss['Fname']}</option>";
        }
    }
    

}
